package datastructure;
import java.util.*;
import ICTCLAS.I3S.AC.ICTCLAS50;
import java.util.regex.*;

import datastructure.word;

public class sentence {
	public String content;
	public String tag_content;
	public int length;
	public ArrayList<word> words; //key->string, value->hashtable
	public ArrayList<word> keyword;    
	public sentence(String a)
	{
		content = a;
		tag_content = new String();
		length = a.length();
		words = new ArrayList<word>();
		keyword = new ArrayList<word>();
		parse();
		//for(int i = 0; i < keyword.size(); i++)
			//System.out.println(keyword.get(i).content);
	}
	
	public void addword(word a)
	{
		if(!words.contains(a))
		{
			words.add(a);
		}
	}
	
	public boolean containsword(word a)
	{
		for(int i = 0; i < words.size(); i++)
		{
			if(words.get(i).pos.equals(a.pos) && words.get(i).content.equals(a.content))
			{
				return true;
			}
		}
		return false;
	}
	
	public boolean containskeyword(word a)
	{
		for(int i = 0; i < keyword.size(); i++)
		{
			if(keyword.get(i).pos.equals(a.pos) && keyword.get(i).content.equals(a.content))
			{
				return true;
			}
		}
		return false;
	}

	//导出关键词和非关键词
	public void extractword()
	{
		String tagcontent = this.tag_content;
		String Words[] = tagcontent.split(" ");
		String Str = "";
		boolean iscombine = false;
		int count = 1;
		//word head = new word("","",0);
		for(int i = 0; i < Words.length; i++)
		{
			String part[] = Words[i].split("/");
			if(part.length == 2)
			{
				word temp = new word(part[0],part[1],count);
				word last;
				if(iskey(part[1],part[0]))
				{
					keyword.add(temp);
				}
				addword(temp);
				count ++;
			}
		}
		return;
	}
	
	public boolean iskey(String pos, String str)
	{
		if(pos.startsWith("n") || (pos.startsWith("v") && !pos.equals("vyou")
				&& !pos.equals("vshi")) || pos.startsWith("t") || pos.startsWith("s")
				|| pos.startsWith("a") || pos.startsWith("z") || (pos.startsWith("m") && (str.length()>1))
				|| pos.equals("r") || pos.equals("f") || pos.equals("d") || pos.equals("qt"))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
	public void parse()
	{
		try
		{
			ICTCLAS50 testICTCLAS50 = new ICTCLAS50();
			String argu = ".";
			//初始化
			if (testICTCLAS50.ICTCLAS_Init(argu.getBytes("UTF-8")) == false)
			{
				System.out.println("Init Fail!");
				return;
			}


			//设置词性标注集(0 计算所二级标注集，1 计算所一级标注集，2 北大二级标注集，3 北大一级标注集)
			testICTCLAS50.ICTCLAS_SetPOSmap(0);


			//导入用户词典前分词
			byte nativeBytes[] = testICTCLAS50.ICTCLAS_ParagraphProcess(content.getBytes("UTF-8"), 0, 1);//分词处理
			//System.out.println(nativeBytes.length);
			String nativeStr = new String(nativeBytes, 0, nativeBytes.length, "UTF-8");
			tag_content = nativeStr;
			//System.out.println("未导入用户词典的分词结果： " + nativeStr);//打印结果
			//处理结果转化
			
			extractword();
			
			///for(int i = 0; i < words.size(); i++)
			//{
				//System.out.println(words.get(i).content+" "+words.get(i).pos+" "+words.get(i).position);
			//}
			
			testICTCLAS50.ICTCLAS_Exit();
		}
		catch (Exception ex)
		{
			System.out.println("parse "+ex);
		}
	}
	
	
}












